package com.ruoyi.mentor.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mentor.bo.SpotAddBo;
import com.ruoyi.mentor.bo.SpotEditBo;
import com.ruoyi.mentor.bo.SpotQueryBo;
import com.ruoyi.mentor.domain.Spot;
import com.ruoyi.mentor.service.ISpotService;
import com.ruoyi.mentor.vo.ExportSpotVo;
import com.ruoyi.mentor.vo.SpotVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 招生名额Controller
 *
 * @author LH
 * @date 2021-05-21
 */
@Api(value = "招生名额控制器", tags = {"招生名额管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mentor/spot")
public class SpotController extends BaseController {

    private final ISpotService iSpotService;

    /**
     * 查询招生名额列表
     */
    @ApiOperation("查询招生名额列表")
    @PreAuthorize("@ss.hasPermi('mentor:spot:list')")
    @GetMapping("/list")
    public TableDataInfo<SpotVo> list(@Validated SpotQueryBo bo) {
        return iSpotService.queryPageList(bo);
    }

    /**
     * 导出招生名额列表
     */
    @ApiOperation("导出招生名额列表")
    @PreAuthorize("@ss.hasPermi('mentor:spot:export')")
    @Log(title = "招生名额", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(@Validated SpotQueryBo bo) {
        List<ExportSpotVo> list = iSpotService.queryList(bo);
        ExcelUtil<ExportSpotVo> util = new ExcelUtil<ExportSpotVo>(ExportSpotVo.class);
        return util.exportExcel(list, "招生名额");
    }

    /**
     * 获取招生名额详细信息
     */
    @ApiOperation("获取招生名额详细信息")
    @PreAuthorize("@ss.hasPermi('mentor:spot:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iSpotService.queryById(id));
    }

    /**
     * 新增招生名额
     */
    @ApiOperation("新增招生名额")
    @PreAuthorize("@ss.hasPermi('mentor:spot:add')")
    @Log(title = "招生名额", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody SpotAddBo bo) {
        return toAjax(iSpotService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改招生名额
     */
    @ApiOperation("修改招生名额")
    @PreAuthorize("@ss.hasPermi('mentor:spot:edit')")
    @Log(title = "招生名额", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@Validated @RequestBody SpotEditBo bo) {
        return toAjax(iSpotService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除招生名额
     */
    @ApiOperation("删除招生名额")
    @PreAuthorize("@ss.hasPermi('mentor:spot:remove')")
    @Log(title = "招生名额" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iSpotService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 审核招生名额
     */
    @ApiOperation("审核招生名额")
    @Log(title = "招生名额" , businessType = BusinessType.DELETE)
    @PostMapping("/examine")
    public AjaxResult examine(@RequestParam("spotId") Long spotId,@RequestParam("status") Integer status) {
        Spot byId = iSpotService.getById(spotId);
        byId.setExamineStatus(status);
        iSpotService.updateById(byId);
        return AjaxResult.success();
    }
}
